<template>
  <div>
    <Spin :spinning="loading" tip="Loading..." :containerStyle="{ minHeight: '60vh' }">
      <FormPanel ref="form" :list="formList" :cols="1" labelWidth="250" formType="add" :isSubmitBtn="false" />
      <div v-if="temp">
        <ButtonArea :containerStyle="{ paddingLeft: '250px' }">
          <AjaxButton :click="formChangeHandle" type="primary">保 存</AjaxButton>
          <!-- <el-button size="small" @click="resetDataBtn">重 置</el-button> -->
        </ButtonArea>
      </div>
    </Spin>
  </div>
</template>

<script>
import { doSearch, doSave, resetData } from '@/api/tool/basic/toolParams';
import { queryInvoiceDic } from '@/api/part/partInfoManage/partParamsView';
import { confirmAction, notifyAction } from '@/utils';
import { dictionary } from '@/utils/dictMixin';

export default {
  name: 'ToolParams',
  mixins: [dictionary],
  data() {
    return { temp: false, loading: false, formList: [] };
  },
  created() {
    console.log(this.$route.meta.ptype, '路由信息');
  },
  mounted() {
    this.getToolParams();
  },
  methods: {
    createFormList(arr) {
      return arr
        .filter(x => x.bVisible === '1')
        .map(x => {
          const target = {
            type: x.editType,
            label: x.paraName,
            fieldName: x.para,
            initialValue: x.editType == 'SELECT' && x.para != 'BJJSZGSR' ? x.bParaValue * 1 : x.bParaValue,
            min: x.minValue,
            max: x.nMaxValue,
            precision: x.nPrecision,
            disabled: x.cAlter === '2' ? true : false,
            style: { width: '200px' },
            descOptions: { content: x.vDesc },
            options: { trueValue: '0', falseValue: '1' },
            itemList: x.dictCode ? this.createDictList(x.dictCode) : []
          };
          return target;
        });
    },

    async getToolParams() {
      this.loading = true;
      try {
        const res = await doSearch({ module: this.$route.meta.ptype ? 'bou' : 'spa' });
        if (res.resultCode === 200) {
          this.formList = this.createFormList(res.data);
          this.temp = true;
          this.queryInvoiceDic();
        }
      } catch (e) {}
      this.loading = false;
    },

    async formChangeHandle() {
      const [err, data] = await this.$refs.form.GET_FORM_DATA();
      if (err) return;
      this.saveToolParams(data);
    },

    async saveToolParams(data) {
      this.loading = true;
      const params = [];
      for (let key in data) {
        params.push({ module: 'spa', para: key, value: data[key] });
      }
      const res = await doSave(params);
      this.loading = false;
      if (res.resultCode === 200) {
        notifyAction('保存成功！', 'success');
        return;
      }
    },

    async resetDataBtn() {
      try {
        await confirmAction('此操作信息无法撤回, 是否重置?', 'warning');
      } catch (e) {}
      this.loading = true;
      const res = await resetData({ module: 'spa' });
      if (res.resultCode === 200) {
        const res1 = await doSearch({ module: 'spa' });
        this.loading = false;
        if (res1.resultCode === 200) {
          this.formList = this.createFormList(res1.data);
          this.temp = true;
          notifyAction('重置成功！!', 'error');
          return;
        }
      } else {
        this.loading = false;
      }
    },

    // 查询发票类型字典项
    async queryInvoiceDic() {
      let res = await queryInvoiceDic();
      if (res.resultCode == 200) {
        this.formList.find(item => item.fieldName == 'BJJSZGSR').itemList = res.data.map(item => ({ text: item.INVOICE_TYPE_NAME, value: item.INVOICE_TYPE_CODE }));
      }
    }
  }
};
// {
//   title: '参数值',
//   dataIndex: 'paraNo',
//   sorter: false,
//   filter: false,
//   filterType: 'input',
//   editable: true,
//   editType: 'text',
//   render: props => {
//     let disabled = false;
//     if (props.row.calter === '0') {
//       disabled = true;
//     }
//     if (props.row.editType === 'INT') {
//       let precision = 0;
//       if (props.row.nmaxValue === '99.99') {
//         precision = 2;
//       }
//       return <el-input-number disabled={disabled} min={props.row.minValue} precision={this.precision} max={props.row.nmaxValue} v-model={props.row.paraNo}></el-input-number>;
//     } else {
//       let checkFlag = true;
//       if (props.row.paraNo === '0') {
//         checkFlag = false;
//       }
//       return <el-checkbox v-model={props.row.paraNo} true-label="1" false-label="0" disabled={disabled} checked={checkFlag}></el-checkbox>;
//     }
//   }
// },
</script>

<style lang="less" scoped>
.center {
  left: 250px;
  /* border: 1px dotted black;
  width: 50%; */
}
</style>
